﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace AnonsaiWPF.CSharp
{
    /// <summary>
    /// Interaction logic for DataBinding.xaml
    /// </summary>
    public partial class DataBinding : Window, IWindow
    {
        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

       DataBindingData data = new DataBindingData();

       public Grid Root
       {
           get
           {
               return this.RootVisual;
           }
       }

       public void SetTheme()
       {
       }
  
        public DataBinding()
        {
            InitializeComponent();

            data.TextValueCurrency = "999.99";
            data.TextValue = "999.99";


            this.CurrencyEditor.PropertyChanged += new PropertyChangedEventHandler(CurrencyEditor_PropertyChanged);
            this.NumberEditor.PropertyChanged += new PropertyChangedEventHandler(NumberEditor_PropertyChanged);
            this.DateTimeEditor.PropertyChanged += new PropertyChangedEventHandler(DateTimeEditor_PropertyChanged);
            this.MaskEditor.PropertyChanged += new PropertyChangedEventHandler(MaskEditor_PropertyChanged);
            this.LayoutRoot.DataContext = data;
      
            this.DateTimeDataItem.Text = "DateTime Editor data item: " + data.TextValueDateTime.ToShortDateString();

            this.Loaded += new RoutedEventHandler(DataBinding_Loaded);
         }

        void MaskEditor_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Value")
            {
                this.PhoneNumber.Text = "Mask Editor data item: " + data.TextValuePhone;
            }
        }

        void DataBinding_Loaded(object sender, RoutedEventArgs e)
        {
          
        }

   
        void DateTimeEditor_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Value")
            {
                this.DateTimeDataItem.Text = "DateTime Editor data item: " + data.TextValueDateTime.ToShortDateString();
            }
        }

        void NumberEditor_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "TextValue")
            {
                this.ValueDataItem.Text = "Number Editor data item: " + data.TextValue;
            }
        }

        void CurrencyEditor_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "TextValue")
            {
                this.CurrencyDataItem.Text = "Currency Editor data item: " + data.TextValueCurrency;             
            }
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (this.None == null)
                return;

            if (this.None.IsChecked.Value)
            {
                this.NumberEditor.SpinType = VIBlend.WPF.Controls.SpinType.None;
                this.CurrencyEditor.SpinType = VIBlend.WPF.Controls.SpinType.None;
            }
            else if (this.Digit.IsChecked.Value)
            {
                this.NumberEditor.SpinType = VIBlend.WPF.Controls.SpinType.SpinDigit;
                this.CurrencyEditor.SpinType = VIBlend.WPF.Controls.SpinType.SpinDigit;
            
            }
            else if (this.DigitWithWrap.IsChecked.Value)
            {
                this.NumberEditor.SpinType = VIBlend.WPF.Controls.SpinType.SpinDigitWithWrap;
                this.CurrencyEditor.SpinType = VIBlend.WPF.Controls.SpinType.SpinDigitWithWrap;
            
            }
            else if (this.ValueBeforeDigit.IsChecked.Value)
            {
                this.NumberEditor.SpinType = VIBlend.WPF.Controls.SpinType.SpinValueBeforeDigit;
                this.CurrencyEditor.SpinType = VIBlend.WPF.Controls.SpinType.SpinValueBeforeDigit;
            }
        }
    }

    public class DataBindingData : INotifyPropertyChanged
    {
        private string textValueCurrency = "0.00";
        private string textValue = "0.00";
        private DateTime dateTime = new DateTime(2001, 1, 1);
        private string phoneNumber = "3123334451";
        public DataBindingData()
        {
        }

        public string TextValuePhone
        {
            get
            {
                return this.phoneNumber;
            }
            set
            {
                this.phoneNumber = value;
                this.OnPropertyChanged("TextValuePhone");
            }
        }

        public DateTime TextValueDateTime
        {
            get
            {
                return this.dateTime;
            }
            set
            {
                this.dateTime = value;
                this.OnPropertyChanged("TextValueDateTime");
            }
        }

        public string TextValueCurrency
        {
            get
            {
                return this.textValueCurrency;
            }
            set
            {
                this.textValueCurrency = value;
                this.OnPropertyChanged("TextValueCurrency");
            }
        }

        public string TextValue
        {
            get
            {
                return this.textValue;
            }
            set
            {
                this.textValue = value;
                this.OnPropertyChanged("TextValue");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion
    }
}
